package dao

import (
	"fmt"
	"hft/Http/Models/request"

	"gorm.io/gorm"
)

type Broker struct{}

func (b *Broker) JustUsername(name string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("username LIKE ?", fmt.Sprintf("%%%v%%", name))
	}
}

func (b *Broker) JustRealName(name string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("real_name LIKE ?", fmt.Sprintf("%%%v%%", name))
	}
}

func (b *Broker) FilterRegion(regionLink string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink))
	}
}

func (b *Broker) GetBrokerList(paginate *request.BrokerPage, regionLink string, accountLink string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Joins("AccountExpand").Preload("LouPan", func(db *gorm.DB) *gorm.DB { return db.Preload("SellLouPanInfo") }).Where("role_id = ?", 33).Scopes(b.FilterRegion(regionLink, db)).Scopes(b.JustRealName(paginate.RealName, db)).Scopes(b.JustUsername(paginate.Username, db)).Scopes(b.Order(db)).Scopes(Paginate(Page(paginate.Page), db))
	}
}

func (b *Broker) Order(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Order("status desc")
	}
}

func (b *Broker) AccountLink(accountLink string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("account_link LIKE ?", accountLink)
	}
}

func (b *Broker) GetAllLouPan(name string, region_link string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	var str = "id,name"
	return func(db *gorm.DB) *gorm.DB {
		if name == "" {
			return db.Where("region_link LIKE ?", fmt.Sprintf("%v%%", region_link)).Select(str)
		} else {
			return db.Where("region_link LIKE ? AND name LIKE ?", fmt.Sprintf("%v%%", region_link), fmt.Sprintf("%v%%", name)).Select(str)
		}
	}
}

var BrokerDao = new(Broker)
